﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedeSocial.ViewModels;
using System.Web.Security;
using RedeSocial.DataAccess;
using RedeSocial.Validation;

namespace RedeSocial.Web
{
	public partial class NotAuthMaster : BaseMasterPage
	{
		#region overrides...
		protected override void CreateViewModel()
		{
			LoginViewModel = new LoginViewModel()
			{
				DefaultLanguage = Session["DefaultLanguage"] as Language,
				FluentValidator = new LoginValidator((Session["DefaultLanguage"] as Language).Id)
			};
		}
		protected override void BindViewModel()
		{
			LoginViewModel.Email = txtUser.Text.Trim();
			LoginViewModel.Password = txtPassword.Text.Trim();
		}
		protected override void BindLabels()
		{
			lblUser.Text = ResourceManager.GetResource(Constants.LOGINVIEW_EMAILLABEL, (Session["DefaultLanguage"] as Language).Id);
			lblPassword.Text = ResourceManager.GetResource(Constants.LOGINVIEW_PASSWORDLABEL, LoginViewModel.DefaultLanguage.Id);
			lblError.Text = ResourceManager.GetResource(Constants.LOGINVIEW_INVALIDUSERMESSAGE, LoginViewModel.DefaultLanguage.Id);
			btnLogin.Text = ResourceManager.GetResource(Constants.LOGINVIEW_LOGINBUTTONTEXT, LoginViewModel.DefaultLanguage.Id);
		}
		#endregion
		#region properties...
		protected LoginViewModel LoginViewModel
		{
			get
			{
				return ViewModel as LoginViewModel;
			}
			set
			{
				ViewModel = value;
			}
		}
		#endregion
		#region event handles...
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Request.IsAuthenticated)
			{
				if (Session["session"] == null)
				{
					string email = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
					User user;
					DataAccess.Session session;
					using (var dataContext = new RedeSocialEntities())
					{
						session = dataContext.Sessions.Include("User").Include("User.Country").Include("User.File").Where(s => s.User.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
						if (session == null)
						{
							user = dataContext.Users.Include("Country").Include("File").Where(u => u.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
							session = new Session()
							{
								ConnectedOn = DateTime.Now,
								LastAccess = DateTime.Now,
								User = user
							};
							dataContext.Sessions.Add(session);
						}
						else
						{
							session.ConnectedOn = DateTime.Now;
							session.LastAccess = DateTime.Now;
						}
						dataContext.SaveChanges();
					}
					Session["session"] = session;
				}
				Response.Redirect("~/default.aspx");

			}
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			if (!IsPostBack)
				lblError.Visible = false;
		}
		protected void btnLogin_Click(object sender, EventArgs e)
		{
			var session = LoginViewModel.Login();
			if (session == null)
				lblError.Visible = true;
			else
			{
				Session.Add("Session", session);
				FormsAuthentication.RedirectFromLoginPage(LoginViewModel.Email, chkRememberMe.Checked);
			}
		}
		#endregion
	}
}